{% extends "snippet_base.html" %}
{% load cab_tags %}
{% load cache %}
{% load ratings_tags %}
{% load safe_markup %}
{% load i18n %}

{% block title %}{{ object.title }}{% endblock %}


{% block app_title %}
<a href="/" class="logo">{% trans "Quantum GIS Code Snippets" %}</a>
{% endblock %}


{% block content %}
  <h2>{{ object.title }}</h2>
    <div class="cab">
    {{ object.highlighted_code|safe }}

    {% if object|more_like_this|length %}
    <div class="cab-related">
        <h3>More like this</h3>
        {% cache 600 mlt object.pk %}
        <ol>
        {% for related in object|more_like_this:5 %}
        <li><a href="{{ related.object.get_absolute_url }}">{{ related.object.title }}</a> by <a href="/users/{{ related.object.author.username }}/">{{ related.object.author.username }}</a>
            {{ related.object.pub_date|timesince }} ago
        </li>
        {% endfor %}
        </ol>
        {% endcache %}
    </div>
    {% endif %}

  </div>
{% endblock %}

{% block sidebar %}
  {{ object.description_html|safe }}
  <dl>
    <dt>Author:</dt>
    <dd><a href="/snippets/users/{{ object.author.username }}/">{{ object.author.username }}</a></dd>
    <dt>Posted:</dt>
    <dd>{{ object.pub_date|date:"F j, Y" }}</dd>
    <dt>Language:</dt>
    <dd><a href="/snippets/{{ object.language.get_absolute_url }}">{{ object.language.name }}</a></dd>
    <dt>QGIS Version:</dt>
    <dd>{{ object.get_version }}</dd>
    <dt>Tags:</dt>
    <dd>{% for tag in object.tags.all %}<a href="{% url "cab_snippet_matches_tag" tag.slug %}">{{ tag.name }}</a> {% endfor %}</dd>
    <dt>Score:</dt>
    <dd>{{ object.rating_score }} (after {{ object.ratings.count }} ratings)</dd>
  </dl>

  <h3>Tools</h3>
  <ul class="menu">
    {% if user.is_authenticated %}
      {% ifequal user.id object.author.id %}
        <li><a href="/snippets/{{ object.id }}/edit/">Edit this snippet</a></li>
      {% endifequal %}
      {% if object|is_bookmarked:request.user %}
        <li>This snippet is in <a href="/bookmarks/">your bookmarks</a></li>
      {% else %}
        <li><a href="/bookmarks/add/{{ object.id }}/">Bookmark this snippet</a></li>
      {% endif %}
      {% with object|rating_score:request.user as rating_score %}
        {% if rating_score %}
          <li>You rated this snippet <strong>{% ifequal rating_score 1 %}useful{% else %}not useful{% endifequal %}</strong>.</li>
        {% else %}
          <li>Rate this snippet: <a href="/snippets/{{ object.id }}/rate/?score=up">useful</a> or <a href="/snippets/{{ object.id }}/rate/?score=down">not useful</a></li>
        {% endif %}
      {% endwith %}
    {% endif %}
    <li><a href="/snippets/{{ object.id }}/download/" type="{{ object.language.mime_type }}">Download this snippet</a></li>
    <li><a href="#comments">Comment on this snippet</a></li>
  </ul>
{% endblock %}


{% block extra_content %}
  <div id="comments">
    <h2>Comments</h2>
    {% load comments %}
    {% get_comment_list for object as comment_list %}
    <dl>
    {% for comment in comment_list %}
      <dt id="c{{ comment.id }}"><a href="/users/{{ comment.user.username }}/">{{ comment.user.username }}</a> (on {{ comment.submit_date|date:"F j, Y" }}):</dt>
      <dd>{{ comment.comment|safe_markdown:"safe"|safe }}
      <p><a href="{{ comment.get_absolute_url }}">#</a></p></dd>
    {% endfor %}
    </dl>
    {% render_comment_form for object %}
  </div>
{% endblock %}
